package com.eleme.search;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Random;

import com.eleme.dbutil.JDBCTools;

public class SearchForEnter {
	public static String Search(String strr,String strrr) throws Exception {
		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement ps = null;
		String str = null;
		//1.获取数据库连接
		conn = JDBCTools.getConnection();
		//3.准备插入的SQL语句
		String sql = "select cellphone from member where cellphone = ? and psword = ? or account = ? and psword = ?";
		//4.执行查询，得到ResultSet
		ps = conn.prepareStatement(sql);
		ps.setString(1,strr);
		ps.setString(2,strrr);
		ps.setString(3,strr);
		ps.setString(4,strrr);
		rs = ps.executeQuery();
		//5.处理ResultSet
		if(rs.next()) {
			str = rs.getString("cellphone");
			
			System.out.println(str);
		}
		//6.关闭连接
		JDBCTools.release(rs, ps,conn);
		return str;
	}
	
	
	public static String Search(String strr) throws Exception {
		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement ps = null;
		String str = null;
		//1.获取数据库连接
		conn = JDBCTools.getConnection();
		//3.准备插入的SQL语句
		String sql = "select cellphone from member where cellphone = ?";
		//4.执行查询，得到ResultSet
		ps = conn.prepareStatement(sql);
		ps.setString(1,strr);
		rs = ps.executeQuery();
		//5.处理ResultSet
		if(rs.next()) {
			str = rs.getString("cellphone");
			
			System.out.println(str);
		}
		//6.关闭连接
		JDBCTools.release(rs, ps,conn);
		return str;
	}
	
	//插入方法
	public static String Insert(String str) throws Exception {
		Connection conn = null;
		Statement statement = null;
		String str1 =  getRandomString(6);
		String str2 = "eleme666";
		//1.获取数据库连接
		conn = JDBCTools.getConnection();
		//3.准备插入的SQL语句
		String sql = "insert into member(account,nickname,psword,cellphone,wallet,redbag)values('"+str+"','"+str1+"','"+str2+"','"+str+"',500.5,2)";
		System.out.println(sql);
		//4.执行插入
		//1)获取操作SQL语句的Statement对象:
		//调用Connection的createStatement()方法来获取
		statement = conn.createStatement();
		//2)调用Statement对象的executeUpdate(sql)执行SQL语句进行插入
		statement.executeUpdate(sql);
		//5.关闭Statement对象
		statement.close();
		//2.关闭连接
		conn.close();
		return str;
	}
	//生成随机字符
	public static String getRandomString(int length) { //length表示生成字符串的长度  
	    String base = "abcdefghijklmnopqrstuvwxyz0123456789";
	    Random random = new Random();
	    StringBuffer sb = new StringBuffer();
	    for (int i = 0; i < length; i++) {
	        int number = random.nextInt(base.length());     
	        sb.append(base.charAt(number));     
	    }
	    return sb.toString();     
	 }
}




